package lumis.portal.localization;

import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.*;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.ITransaction;

import java.sql.*;
import java.util.*;

/**
 * DAO class that read custom strings from database
 * 
 * @version $Revision: 8944 $ $Date: 2008-02-29 12:30:48 -0300 (Fri, 29 Feb 2008) $
 * @since 4.0.0
 */
public class CustomStringLocalizationDaoJdbc implements ICustomStringLocalizationDao
{
	public Map<String, String> getStringTable(String resourcePath, Locale locale) throws DaoException
	{
		Map<String, String> stringTable = new HashMap<String, String>();
		try
		{
			ITransactionJdbc transaction = (ITransactionJdbc) PortalTransactionFactory.createTransaction();
			try
			{
				transaction.begin();

				Connection connection = transaction.getConnection();
				String strQuery = "SELECT stringId, string FROM lum_CustomString WHERE resourcePath=? AND localeCode=? AND status=1";
				PreparedStatement statement = connection.prepareStatement(strQuery);
				try
				{
					statement.setString(1, resourcePath);
					statement.setString(2, locale.toString());
					ResultSet resultSet = statement.executeQuery();
					try
					{
						while (resultSet.next())
							stringTable.put(resultSet.getString("stringId"), resultSet.getString("string"));
					}
					finally
					{
						resultSet.close();
					}
				}
				finally
				{
					statement.close();
				}

				transaction.commit();
			}
			finally
			{
				transaction.dispose();
			}
		}
		catch (Exception e)
		{
			throw new DaoException("STR_UNEXPECTED_EXCEPTION", e);
		}

		return stringTable;
	}
	
	public void addOrUpdate(CustomStringConfig customStringConfig, ITransaction transaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			int rowsAffected = 1;
			
			PreparedStatement statement = connection.prepareStatement("update lum_CustomString set stringId = ?," + 
					" string = ?, resourcePath = ?, localeCode = ?, status = ? where customStringId = ?");
			
			try
			{
				statement.setString(1, customStringConfig.getStringId());
				statement.setString(2, customStringConfig.getString());
				statement.setString(3, customStringConfig.getResourcePath());
				statement.setString(4, customStringConfig.getLocaleCode());
				statement.setInt(5, customStringConfig.getStatus());
				statement.setString(6, customStringConfig.getCustomStringId());

				rowsAffected = statement.executeUpdate();
			}
			finally
			{
				statement.close();
			}

			if (rowsAffected == 0)
			{
				try
				{
					statement = connection.prepareStatement("insert into lum_CustomString (customStringId, stringId, string, resourcePath, localeCode, status)"
							+ " values (?, ?, ?, ?, ?, ?) ");

					statement.setString(1, customStringConfig.getCustomStringId());
					statement.setString(2, customStringConfig.getStringId());
					statement.setString(3, customStringConfig.getString());
					statement.setString(4, customStringConfig.getResourcePath());
					statement.setString(5, customStringConfig.getLocaleCode());
					statement.setInt(6, customStringConfig.getStatus());

					statement.executeUpdate();
				}
				finally
				{
					statement.close();
				}
			}
		}
		catch (DaoException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public CustomStringConfig get(String customStringId, ITransaction transaction) throws PortalException
	{
		try
		{
			CustomStringConfig loadedConfig = null;
			
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement("SELECT * FROM lum_CustomString WHERE customStringId = ?");
			statement.setString(1, customStringId);
			
			try
			{
				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
					{
						loadedConfig = new CustomStringConfig();
						loadedConfig.setStringId(resultSet.getString("stringId"));
						loadedConfig.setString(resultSet.getString("string"));
						loadedConfig.setResourcePath(resultSet.getString("resourcePath"));
						loadedConfig.setLocaleCode(resultSet.getString("localeCode"));
						loadedConfig.setStatus(resultSet.getInt("status"));
					}
				}
				finally
				{
					resultSet.close();
				}
			}
			finally
			{
				statement.close();
			}

			return loadedConfig;
		}
		catch (DaoException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public void delete(String customStringId, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
	
			PreparedStatement statement = connection.prepareStatement("DELETE FROM lum_CustomString WHERE customStringId = ?");
			
			statement.setString(1, customStringId);
			
			statement.executeUpdate();
			statement.close();
		}
		catch(PortalException e)
		{
			throw e;
		}
		catch(Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public String getIdByStringInfo(CustomStringConfig customStringConfig, ITransaction transaction) throws PortalException
	{
		String customStringId = null;

		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select customStringId from lum_CustomString where stringId = ? and resourcePath = ? and localeCode = ?");
			try
			{
				statement.setString(1, customStringConfig.getStringId());
				statement.setString(2, customStringConfig.getResourcePath());
				statement.setString(3, customStringConfig.getLocaleCode());

				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
						customStringId = resultSet.getString("customStringId");
				}
				finally
				{
					resultSet.close();
				}
				
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
		
		return customStringId;
	}
}
